package module.bank.impl;
import common.model.Entity.bankEntity.Account;
import common.model.Entity.bankEntity.Transaction;
import dao.BankDao.AccountDao;
import dao.BankDao.TransactionDao;
import dao.impl.BankDaoImpl.AccountDaoImpl;
import dao.impl.BankDaoImpl.TransactionDaoImpl;
import module.bank.BankAccountService;
import module.bank.BankTransactionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import network.protocol.Response;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

public class BankTransactionServiceImpl implements BankTransactionService {
    private static final Logger logger = LoggerFactory.getLogger(BankTransactionServiceImpl.class);
    private final TransactionDao transactionDao;

    private static final BankTransactionServiceImpl INSTANCE = new BankTransactionServiceImpl();

    public static BankTransactionServiceImpl getInstance() {
        return INSTANCE;
    }

    public BankTransactionServiceImpl() {
        this.transactionDao = new TransactionDaoImpl(); // 假设有TransactionDaoImpl实现
    }

    public BankTransactionServiceImpl(TransactionDao transactionDao) {
        this.transactionDao = transactionDao;
    }

    /**
     * 获取账户交易记录
     * @param accountId 账户ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param transactionType 交易类型
     * @return 包含交易记录的响应
     */
    public Response<List<Transaction>> getTransactions(Long accountId, LocalDateTime startTime, LocalDateTime endTime, Integer transactionType) {
        try {
            // 参数验证
            if (accountId == null || accountId <= 0) {
                logger.warn("获取交易记录失败: 账户ID无效, accountId={}", accountId);
                return Response.error("账户ID无效");
            }

            if (startTime != null && endTime != null && startTime.isAfter(endTime)) {
                logger.warn("获取交易记录失败: 时间范围无效, startTime={}, endTime={}", startTime, endTime);
                return Response.error("时间范围无效，开始时间不能晚于结束时间");
            }

            List<Transaction> transactions = transactionDao.findTransactionsByAccountId(accountId, startTime, endTime, transactionType);
            logger.info("成功获取交易记录: accountId={}, 记录数={}", accountId, transactions.size());
            return Response.success(transactions);
        } catch (Exception e) {
            logger.error("获取交易记录异常: accountId={}, startTime={}, endTime={}, type={}",
                    accountId, startTime, endTime, transactionType, e);
            return Response.error("获取交易记录失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户的所有交易记录
     * @param accountId 账户ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 包含交易记录的响应
     */
    public Response<List<Transaction>> getAccountTransactions(Long accountId, LocalDateTime startTime, LocalDateTime endTime) {
        try {
            if (accountId == null || accountId <= 0) {
                logger.warn("获取账户交易记录失败: 账户ID无效, accountId={}", accountId);
                return Response.error("用户ID无效");
            }

            if (startTime != null && endTime != null && startTime.isAfter(endTime)) {
                logger.warn("获取用户交易记录失败: 时间范围无效, startTime={}, endTime={}", startTime, endTime);
                return Response.error("时间范围无效，开始时间不能晚于结束时间");
            }

            List<Transaction> transactions = transactionDao.findTransactionsByAccountId(accountId, startTime, endTime,1);
            logger.info("成功获取账户交易记录: accountId={}, 记录数={}", accountId, transactions.size());
            return Response.success(transactions);
        } catch (Exception e) {
            logger.error("获取用户交易记录异常: userId={}, startTime={}, endTime={}",
                    accountId, startTime, endTime, e);
            return Response.error("获取账户交易记录失败: " + e.getMessage());
        }
    }

    /**
     * 创建交易记录
     * @param transaction 交易对象
     * @return 包含操作结果的响应
     */
    public Response<Boolean> createTransaction(Transaction transaction) {
        try {
            if (transaction == null) {
                logger.warn("创建交易记录失败: 交易对象为空");
                return Response.error("交易对象不能为空");
            }

            if (transaction.getAccountId() == null || transaction.getAccountId() <= 0) {
                logger.warn("创建交易记录失败: 账户ID无效, accountId={}", transaction.getAccountId());
                return Response.error("账户ID无效");
            }

            if (transaction.getAmount() == null || transaction.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
                logger.warn("创建交易记录失败: 交易金额无效, amount={}", transaction.getAmount());
                return Response.error("交易金额必须大于0");
            }

            boolean success = transactionDao.insertTransaction(transaction);
            if (success) {
                logger.info("成功创建交易记录: transactionId={}, accountId={}, amount={}",
                        transaction.getId(), transaction.getAccountId(), transaction.getAmount());
                return Response.success(true);
            } else {
                logger.error("创建交易记录失败: 数据库操作失败");
                return Response.error("创建交易记录失败");
            }
        } catch (Exception e) {
            logger.error("创建交易记录异常: accountId={}, amount={}",
                    transaction != null ? transaction.getAccountId() : "null",
                    transaction != null ? transaction.getAmount() : "null", e);
            return Response.error("创建交易记录失败: " + e.getMessage());
        }
    }
}
